//
//  HZOAuthVM.m
//  微博 OC
//
//  Created by 黑眼圈 on 16/4/21.
//  Copyright © 2016年 heiyan. All rights reserved.
//

#import "HZOAuthVM.h"
#import "HZNetTools.h"

// APPKEY
static NSString* WB_APP_KEY = @"2817770965";
// 回调页一定要与在开放平台上面设置的一模一样
static NSString* WB_REDIRECT_URI = @"http://www.itcast.cn";
// APP_SECRET
static  NSString*   WB_APP_SECRET = @"023572891f16991d3fdbcac91dd91b19";

@implementation HZOAuthVM


#pragma mark -
#pragma mark     判断是否登录
-(BOOL)isslogined{
    
    //如果没有获取到access_token
    if (self.model.access_token == nil) {
        
        return NO;
    }
    
    
    return YES;
}


//初始化的构造方法
-(instancetype)init{
    
    if (self = [super init]) {
        
        //反归档
        self.model = [self unarchive];
    }
    
    return self;
}


#pragma mark -
#pragma mark     单例对象
+(instancetype)sharedOAuth{
    
    static HZOAuthVM* oauth;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        oauth = [[HZOAuthVM alloc]init];
    });
    
    return oauth;
}




#pragma mark -
#pragma mark     获取access_token
-(void)giveToken:(NSString*)code :(void(^)(BOOL succ))finishedBlock{
    
    NSString* urlStirng = @"https://api.weibo.com/oauth2/access_token";
    
    
    
    NSDictionary* paras = @{
                            @"client_id": WB_APP_KEY,
                            @"client_secret": WB_APP_SECRET,
                            @"grant_type": @"authorization_code",
                            @"code": code,
                            @"redirect_uri": WB_REDIRECT_URI,
                            };
    
    //网络 post 请求
    HZNetTools* tools = HZNetTools.sharedTools;
    
    [tools requestWithMethod:HZRequestMethodPost urlString:urlStirng parameters:paras finishedBlock:^(id responstObject, NSError *error) {
        
        if (error != nil) {
            NSLog(@"access_token请求失败");
            
            //失败的 block 回调
            finishedBlock(NO);
            
            return;
        }
        
        
        //保存到字典中
        HZOAuthModel* model = [HZOAuthModel oauthWithDict:responstObject];
        
        self.model = model;
        
        //获取个人信息
        [self giveOwnMess:model :finishedBlock];
        
    }];
    
}


#pragma mark -
#pragma mark     获取个人信息
-(void)giveOwnMess:(HZOAuthModel*)model :(void(^)(BOOL succ))finishedBlock{
    
    NSString* urlStirng = @"https://api.weibo.com/2/users/show.json";
    
    NSString* token = model.access_token;
    NSString* uid = model.uid;
    
    //    NSLog(@"%@\n%@",token,uid);
    
    NSDictionary* paras = @{@"access_token":token,@"uid":uid};
    
    
    [[HZNetTools sharedTools] requestWithMethod:HZRequestMethodGet urlString:urlStirng parameters:paras finishedBlock:^(id responstObject, NSError *error) {
        
        if (error != nil) {
            
            NSLog(@"请求个人信息失败");
//            NSLog(@"%@",error);
            
            //失败的 block 回调
            finishedBlock(NO);
            
            return;
        }
        
//                NSLog(@"%@",responstObject);
        
        self.model.name = responstObject[@"name"];
        self.model.profile_image_url = responstObject[@"profile_image_url"];
        
//        NSLog(@"%@",self.model.profile_image_url);
        
        //归档
        [self archive:self.model];
        
        //成功的 block 回调
        finishedBlock(YES);
        
        
        
        
    }];
    
    
}

//归档
-(void)archive:(HZOAuthModel*)model{
    
    //拼接处沙盒路径
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"model.archive"];
    
    [NSKeyedArchiver archiveRootObject:model toFile:path];
}


//反归档
-(HZOAuthModel*)unarchive{
    
    HZOAuthModel* model = [[HZOAuthModel alloc]init];
    
    //拼接处沙盒路径
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"model.archive"];
    
    
    model = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    return model;
    
}








@end
